﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using agvcontroller.Common.DataBase;
using agvcontroller.Log;

namespace agvcontroller.Common
{
    public class CNode
    {
        public int ID
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public int X
        {
            get;
            set;
        }

        public int Y
        {
            get;
            set;
        }

        public int RealX
        {
            get;
            set;
        }

        public int RealY
        {
            get;
            set;
        }

        public bool UseNode
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

        public int Occupied_Vechile
        {
            get;
            set;
        }

        public int Reserved_Vechile
        {
            get;
            set;
        }

        public string NodeType
        {
            get;
            set;
        }
    }

    public class CNodeDic: Dictionary<int, CNode>
    {
        private static CNodeDic uniqueInstance;
        CAGVCDB AGVCDB;
        Logger Log;

        public static CNodeDic getInstance()
        {
            if (uniqueInstance == null)
            {
                uniqueInstance = new CNodeDic();
            }
            return uniqueInstance;
        }

        public CNodeDic()
        {
            AGVCDB = CAGVCDB.getInstance();

            Log = new Logger("CNodeDic");

            GetDataFromDB();
        }

        public bool IsInvalidNodeNo(int nodeno)
        {
            if (!this.ContainsKey(nodeno))
                return false;
            else 
                return true;
        }

        public int GetDataFromDB()
        {
            DataTable table =  AGVCDB.AGVCDataSet.Tables["NodeList"];

            CNode node;

            try
            {
                foreach (DataRow row in table.Rows)
                {
                    node = new CNode();
                    node.ID = Convert.ToInt32(row["ID"]);
                    node.Name = Convert.ToString(row["Name"]);
                    node.UseNode = Convert.ToBoolean(row["UseNode"]);
                    node.Description = Convert.ToString(row["Description"]);
                    node.X = Convert.ToInt32(row["X"]);
                    node.Y = Convert.ToInt32(row["Y"]);
                    node.RealX = Convert.ToInt32(row["RealX"]);
                    node.RealY = Convert.ToInt32(row["RealY"]);
                    node.Occupied_Vechile = Convert.ToInt32(row["Occupied_Vechile"]);
                    node.Reserved_Vechile = Convert.ToInt32(row["Reserved_Vechile"]);
                    node.NodeType = Convert.ToString(row["NodeType"]);

                    this.Add(node.ID, node);
                }
            }
            catch (Exception ex)
            {
                Log.Exception(string.Format("{0},{1}",ex.Message,ex.StackTrace));
                return CReturnCode.ERR_DATA_ACQUIRING_FAIL;
            }

            return CReturnCode.SUCCESS;
        }

        public int SetDataToDB()
        {
            return CReturnCode.SUCCESS;
        }
    }
}
